<?php
App::uses('AppModel', 'Model');

class Mensagem extends AppModel {
/**
 * Use table
 *
 * @var mixed False or table name
 */
	public $useTable = 'mensagem';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'anuncio_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'usuario_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'texto' => array(
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'nome_contato' => array(
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'email_contato' => array(
			'email' => array(
				'rule' => array('email'),
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Anuncio' => array(
			'className' => 'Anuncio',
			'foreignKey' => 'anuncio_id',
		),
		'Usuario' => array(
			'className' => 'Usuario',
			'foreignKey' => 'usuario_id',
		)
	);
	
	public function isLida(){
		return $this->data['Mensagem']['lida'];
	}
	
	public function marcarComoLida(){
		$this->save(array('lida'=>1));
	}
	
	public function numeroNaoLidas($id){
		return $this->find('count', array('conditions'=>array('Mensagem.usuario_id'=>$id, 'lida'=>0)));
	}
}
